package com.sn.author.monitor.handler;

import com.alibaba.fastjson.JSON;
import com.sn.author.common.context.SpringContext;
import com.sn.author.common.exception.BadRequestException;
import com.sn.author.common.sdk.AbstractResponse;
import com.sn.author.common.sdk.entity.AbstractResult;
import com.sn.author.common.sdk.response.AttentionResponse;
import com.sn.author.common.sdk.response.LikeResponse;
import com.sn.author.domain.entity.ImportAccount;
import com.sn.author.domain.entity.JobAccount;
import com.sn.author.domain.taskEntity.TaskModel;
import com.sn.author.service.impl.DyUserServiceImpl;
import lombok.extern.slf4j.Slf4j;

/**
 * @Author xiaom
 * @Date 2020/9/22 17:13
 * @Version 1.0.0
 * @Description <>
 **/
@Slf4j
public class LikeHandler extends AbsHandler {
    public LikeHandler(TaskModel taskModel) {
        super(taskModel);
    }

    @Override
    public void execute() throws Exception {
        super.execute();
    }

    @Override
    public void responseAfter(AbstractResponse response) {
        JobAccount jobAccount = JSON.parseObject(taskModel.getTaskParam().toString(),JobAccount.class);
        getAccountService().updateLikeCount(jobAccount.getAccount(),1);
    }

    public LikeResponse requestApi(){
        //需要的mapper服务
        DyUserServiceImpl dyUserService = SpringContext.getBean(DyUserServiceImpl.class);
        JobAccount jobAccount = JSON.parseObject(taskModel.getTaskParam().toString(),JobAccount.class);

        //获取账号信息
        ImportAccount account = dyUserService.getDyUserInfo(jobAccount.getAccount());
        if(account == null){
            throw new BadRequestException("执行点赞任务失败 账号:【" + jobAccount.getAccount() + "】不存在");
        }
        log.info("【抖音点赞】video_url:{}",jobAccount.getVideoUrl());
        return getSdkService().executeLikeSdk(account.getUrlParam(),account.getHeader(),jobAccount.getAccount(),jobAccount.getVideoUrl(),account.getAreaCode());
//        return testResponse();
    }

    private LikeResponse testResponse(){
        LikeResponse response = new LikeResponse();
        response.setCode("200");
        response.setSuccess(true);
        response.setUrl("baidu.com");

        LikeResponse.ResponseMsgResult result = new LikeResponse.ResponseMsgResult();
        result.setMessage("成功");
        result.setPhone("18224449006");
        result.setStatus(true);
        result.setVideoUrl("www.google.com");

        response.setData(result);
        return response;
    }

    @Override
    String getSuccessMessage() {
        JobAccount jobAccount = JSON.parseObject(taskModel.getTaskParam().toString(),JobAccount.class);
        return "账号：" + jobAccount.getAccount() + " 为视频链接:" + jobAccount.getVideoUrl() + " 点赞成功";
    }

    @Override
    String getErrorMessage(String errmsg) {
        JobAccount jobAccount = JSON.parseObject(taskModel.getTaskParam().toString(),JobAccount.class);
        return "账号：" + jobAccount.getAccount() + " 为视频链接:" + jobAccount.getVideoUrl() + " 点赞失败 错误信息:" + errmsg;
    }

    @Override
    public void responseErrAfter(String errMsg) {

    }
}
